package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.SysMedicines;
import com.ruoyi.system.domain.vo.SysPrescriptionsVo;
import com.ruoyi.system.mapper.SysMedicinesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysPrescriptionsMapper;
import com.ruoyi.system.domain.SysPrescriptions;
import com.ruoyi.system.service.ISysPrescriptionsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 销售信息Service业务层处理
 * 
 * @author jiashu
 * @date 2025-03-13
 */
@Service
public class SysPrescriptionsServiceImpl implements ISysPrescriptionsService 
{
    @Autowired
    private SysPrescriptionsMapper sysPrescriptionsMapper;
    @Autowired
    private SysMedicinesMapper sysMedicinesMapper;

    /**
     * 查询销售信息
     * 
     * @param prescriptionId 销售信息主键
     * @return 销售信息
     */
    @Override
    public SysPrescriptions selectSysPrescriptionsByPrescriptionId(Long prescriptionId)
    {
        return sysPrescriptionsMapper.selectSysPrescriptionsByPrescriptionId(prescriptionId);
    }

    /**
     * 查询销售信息列表
     * 
     * @param sysPrescriptions 销售信息
     * @return 销售信息
     */
    @Override
    public List<SysPrescriptions> selectSysPrescriptionsList(SysPrescriptions sysPrescriptions)
    {
        return sysPrescriptionsMapper.selectSysPrescriptionsList(sysPrescriptions);
    }

    /**
     * 新增销售信息
     * 
     * @param sysPrescriptions 销售信息
     * @return 结果
     */
    @Override
    public int insertSysPrescriptions(SysPrescriptions sysPrescriptions)
    {
        return sysPrescriptionsMapper.insertSysPrescriptions(sysPrescriptions);
    }

    /**
     * 修改销售信息
     * 
     * @param sysPrescriptions 销售信息
     * @return 结果
     */
    @Override
    public int updateSysPrescriptions(SysPrescriptions sysPrescriptions)
    {
        return sysPrescriptionsMapper.updateSysPrescriptions(sysPrescriptions);
    }

    /**
     * 归档
     * @param sysPrescriptions
     * @return
     */
    @Override
    @Transactional
    public int archivedPrescriptions(SysPrescriptions sysPrescriptions) {
        // 修改药品库存
        SysMedicines sysMedicines = sysMedicinesMapper.selectSysMedicinesByMedicineId(sysPrescriptions.getMedicineId());
        Double stockQuantity = sysMedicines.getStockQuantity();
        Double quantityPrescribed = sysPrescriptions.getQuantityPrescribed();
        sysMedicines.setStockQuantity(stockQuantity-quantityPrescribed);
        sysMedicinesMapper.updateSysMedicines(sysMedicines);

        sysPrescriptions.setIsArchived("1");
        return sysPrescriptionsMapper.updateSysPrescriptions(sysPrescriptions);
    }

    /**
     * 批量删除销售信息
     * 
     * @param prescriptionIds 需要删除的销售信息主键
     * @return 结果
     */
    @Override
    public int deleteSysPrescriptionsByPrescriptionIds(Long[] prescriptionIds)
    {
        return sysPrescriptionsMapper.deleteSysPrescriptionsByPrescriptionIds(prescriptionIds);
    }

    /**
     * 删除销售信息信息
     * 
     * @param prescriptionId 销售信息主键
     * @return 结果
     */
    @Override
    public int deleteSysPrescriptionsByPrescriptionId(Long prescriptionId)
    {
        return sysPrescriptionsMapper.deleteSysPrescriptionsByPrescriptionId(prescriptionId);
    }
}
